package com.example.tourapp.util;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Util_Internet {

       public static void Get_ok(String url, final Context context)
       {
               OkHttpClient client=new OkHttpClient();
               Request request=new Request.Builder()
                       .url("http://apis.juhe.cn/simpleWeather/query?key=cfb3dd81d69171bec0e0d2fedbd35386&city="+url)
                       .get()
                       .build();
           client.newCall(request).enqueue(new Callback() {
                   @Override
                   public void onFailure(Call call, IOException e) {
                       ((Activity)context).runOnUiThread(()->{
                           Toast.makeText(context, "网络错误，请检查网络是否开启！", Toast.LENGTH_SHORT).show();
                       });

                   }

               @Override
               public void onResponse(Call call, Response response) throws IOException {
                   String data = response.body().string();
                   EventBus.getDefault().post(data);
                   ((Activity)context).runOnUiThread(()->{
                       Toast.makeText(context, "刷新成功！", Toast.LENGTH_SHORT).show();
                   });
               }
           });

       }

}
